<?php
namespace app\home\controller;
header("Content-type: text/html; charset=utf-8");
use think\Controller;
use think\Db;
use think\Cache;
use think\Validate;
use app\admin\model\User;
use app\admin\model\Donation;
class Lesson extends Controller
{

    public function index(){
        $id= $_POST['id'];
        $lessons =Db::name('lesson');
        $lesson = $lessons
            ->where(array('book_lesson.id' => $id))
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->field('book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.id,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city,book_lesson.lesson_cost_old')
            ->find();
        echo json_encode($lesson);

    }
    public function tuition_lesson(){//学费课程展示列表
        $id= $_POST['lesson_sn'];
        $lessons =Db::name('lesson');
        $lesson = $lessons
            ->where(array('book_lesson.lesson_sn' => $id,'cost_type'=>1 , 'is_approve'=>1))
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->field('book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.id,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city,book_lesson.lesson_cost_old')
            ->find();
        echo json_encode($lesson);

    }
    //学币课程的展示列表
    public function coin_lesson(){
        $id= $_POST['lesson_sn'];
        $lessons =Db::name('lesson');
        $lesson = $lessons
            ->where(array('book_lesson.lesson_sn' => $id,'cost_type'=>2 , 'is_approve'=>1))
            ->join(' book_subject ','subject_id=book_subject.id','LEFT')
            ->join(' book_lesson_program ','program_id=book_lesson_program.id','LEFT')
            ->join(' book_service_target ','service_target_id=book_service_target.id','LEFT')
            ->join(' book_lesson_nature ','lesson_nature_id=book_lesson_nature.id','LEFT')
            ->join(' book_user ','teacher_id=book_user.id','LEFT')
            ->join(' book_classroom ','classroom_id=book_classroom.id','LEFT')
            ->join(' book_city ','book_lesson.city_id=book_city.id','LEFT')
            ->field('book_lesson.lesson_sn,book_subject.subject,book_lesson_program.program,book_service_target.service_target,book_lesson_nature.lesson_nature,book_user.full_name,book_user.sex,book_user.age,book_classroom.classroom_address,book_lesson.lesson_method,book_lesson.is_comment,book_lesson.lesson_cost,book_lesson.teacher_group_description,book_lesson.lesson_feature,book_lesson.cost_type,book_lesson.id,book_lesson.add_description,book_lesson.teacher_description,book_lesson.classroom_description,book_lesson.lesson_name,book_lesson.adapt_crowd,book_lesson.cost_type_description,book_lesson.lesson_time_description,book_lesson.teacher_group_images,book_lesson.lesson_num,book_city.city')
            ->find();
        echo json_encode($lesson);

    }
    //年级下拉列表
    public function grade_select()
    {
        //年级下拉选项
        $grade_list =Db::name('grade');
        $grades = $grade_list->select();
        //dump($grades);exit;
       return json($grades);
    }

    //根据选择年级查询学币课
    public function find_coin(){
        $true['status'] = 1;
        $false['status'] = 0;
        $_GET['grade_id'] = 2;

        $data = $_GET['grade_id'];
        $page = $_GET['p'];
        if (!empty($data)) {
            $lesson_detail = Db::name('lesson');
            //数据交汇分页
            $lesson_details = $lesson_detail
                ->join(' book_subject ', 'subject_id=book_subject.id', 'LEFT')
                ->join(' book_lesson_program ', 'program_id=book_lesson_program.id', 'LEFT')
                ->field('book_lesson.id as lessonid,book_lesson.lesson_sn,book_lesson.grade_id,book_lesson.lesson_cost,book_subject.subject,book_lesson_program.program')
                ->where('cost_type=2 AND is_approve=1 AND book_lesson.com_lesson_num=0')
                ->select();
            //dump($lesson_details);exit;
            if (!empty($lesson_details)) {
                foreach ($lesson_details as $key => $val) {
                    $lesson_details[$key]['grade_arr'] = explode(',', $lesson_details[$key]['grade_id']);
                    if (in_array($data, $lesson_details[$key]['grade_arr'])) {
                        $lesson[$key] = $lesson_details[$key];
                        $lessons[] = $lesson_detail
                            ->join(' book_subject ', 'subject_id=book_subject.id', 'LEFT')
                            ->join(' book_lesson_program ', 'program_id=book_lesson_program.id', 'LEFT')
                            ->field('book_lesson.id as lessonid,book_lesson.lesson_sn,book_lesson.grade_id,book_lesson.lesson_cost,book_lesson.lesson_name')
                            ->where('book_lesson.id="' . $lesson_details[$key]['lessonid'] . '" AND cost_type=2 AND is_approve=1 AND book_lesson.com_lesson_num=0')
                            ->find();
                        foreach ($lessons as $k => $v) {
                            $count = 6;
                            $page = (empty($page)) ? '1' : $page; //判断当前页面是否为空 如果为空就表示为第一页面
                            $start = ($page - 1) * $count; //计算每次分页的开始位置
                            $totals = count($lessons);
                            $countpage = ceil($totals / $count); //计算总页面数
                            $pagedata = array_slice($lessons, $start, $count);
                        }
                    }
                }
            }else{
                return json($true);

            }
        }

        //$this->ajaxReturn($lesson);
        if (!empty($pagedata)) {
            return json($pagedata);
        } else {
            return json($true);
        }


    }
    //根据选择年级查询学费课
    public function find_tuition()
    {   $true['status'] = 1;
        $false['status'] = 0;
        $data = $_GET['grade_id'];
        $page = $_GET['p'];
        if (!empty($data)) {
            $lesson_detail = Db::name('lesson');
            //数据交汇分页
            $lesson_details = $lesson_detail
                ->join(' book_subject ', 'subject_id=book_subject.id', 'LEFT')
                ->join(' book_lesson_program ', 'program_id=book_lesson_program.id', 'LEFT')
                ->field('book_lesson.id as lessonid,book_lesson.lesson_sn,book_lesson.grade_id,book_lesson.lesson_cost,book_subject.subject,book_lesson_program.program')
                ->where('cost_type=1 AND is_approve=1 AND book_lesson.com_lesson_num=0')
                ->select();
            //dump($lesson);exit;
            if (!empty($lesson_details)) {
            foreach ($lesson_details as $key => $val) {
                $lesson_details[$key]['grade_arr'] = explode(',', $lesson_details[$key]['grade_id']);
                if (in_array($data, $lesson_details[$key]['grade_arr'])) {
                    $lesson[$key] = $lesson_details[$key];
                    $lessons[] = $lesson_detail
                        ->join(' book_subject ', 'subject_id=book_subject.id', 'LEFT')
                        ->join(' book_lesson_program ', 'program_id=book_lesson_program.id', 'LEFT')
                        ->field('book_lesson.id as lessonid,book_lesson.lesson_sn,book_lesson.grade_id,book_lesson.lesson_cost,book_lesson_program.program_full_name,book_lesson.lesson_name')
                        ->where('book_lesson.id="' . $lesson_details[$key]['lessonid'] . '" AND cost_type=1 AND is_approve=1 AND book_lesson.com_lesson_num=0')
                        ->find();
                    foreach ($lessons as $k => $v) {
                        $count = 6;
                        $page = (empty($page)) ? '1' : $page; //判断当前页面是否为空 如果为空就表示为第一页面
                        $start = ($page - 1) * $count; //计算每次分页的开始位置
                        $totals = count($lessons);
                        $countpage = ceil($totals / $count); //计算总页面数
                        $pagedata = array_slice($lessons, $start, $count);
                    }
                }
            }
        }else{
                return json($true);
            }
        }
        //dump(empty($pagedata));
        //$this->ajaxReturn($lesson);
       // return $countpage;
        if (!empty($pagedata)) {
            return json($pagedata);
        } else {
            return json($true);
        }
    }


    //捐赠信息查重
    public function find_donation(){
        $status0['status']=0;
        $status1['status']=1;
        if(request()->isPost()){
            $donations=Db::name('donation');
            $info=$donations
                ->where('tel='.$_POST['donation_tel'].' AND donation_book="'.$_POST['donation_book'].'" AND full_name="'.$_POST['donation_name'].'"')
                ->join(' book_user ','donation_id=book_user.id','LEFT')
                ->find();
            if(!empty($info)){
               return json($info);
            }else{
               return json($status1);
            }
        }

    }
    //添加捐赠信息
    public function add_donation(){
        if (request()->isPost()) {
            $data['donation_book'] = trim($_POST['donation_book']);//捐赠书名
            $data['donation_time'] = time();//捐赠时间
            $data['donation_collect_id'] = $_POST['donation_collect_id'];
            $price = trim($_POST['donation_price']);//获取捐赠书价
            if ($price) { //制作学币
                $data['donation_price'] = $price;//将书价存入数组
                $coin = ceil($price * 2);//书价的2倍向上取整
                if ($coin % 10 == 0) { //判定2倍向上取整后的书价能否对10取余，
                    //能则将2倍向上取整后的书价赋给学币，不能则
                    //先将10减去余数，余下的加上2倍向上取整后的书价
                    //所获得的数为学币数
                    $data['get_coin'] = $coin;
                } else {
                    $coin = $coin + (10 - $coin % 10);
                    $data['get_coin'] = $coin;

                }
            }
            $donations=new Donation();
            //查询会员信息
            $info =Db::name('user')->where(array('tel' => $_POST['donation_tel']))->find();

            if ($info) { //判定是否为会员
                $create =$donations->insertGetId($data);
                $d_id = $info['id'];//将会员id赋值给一个变量
                $creates['donation_id'] = $d_id;
                //1.根据基于user_id查询捐赠记录信息
                $d_info =Db::name('donation')->where(array('donation_id' => $creates['donation_id']))->find();
                //2.判定是否有捐赠记录有捐书记录信息，则将当前的记录信息存入记录表当中
                if ($d_info) {
                    if ($create) {
                        $rec =Db::name('donation')->where(array('id' => $create))->update($creates);
                        if ($rec) {
                           echo json_encode(1);
                        } else {
                            echo json_encode(0);
                        }
                    }
                } else { //没有捐赠记录
                    if ($create) {
                        $rec =Db::name('donation')->where(array('id' => $create))->insert($creates);
                        if ($rec) {
                            $coin = array();
                            $coin['receive_id'] = $create['donation_id'];
                            $coin['month_coin'] =  500;
                            $d_rec =Db::name('coin')->where(array('receive_id' => $create['donation_id']))->insert($coin);
                            //echo json_encode($d_rec);
                           if ($d_rec) {
                                echo json_encode(1);
                            }else{
                                echo json_encode(2);
                            }
                        } else {
                            echo json_encode(0);
                        }
                    }
                }
            } else {//不是会员
                $user = array();
                $user['rec_id']=1;
                $user['full_name']=$_POST['donation_name'];
                $user['tel']=$_POST['donation_tel'];
                $user['ctime']=$data['donation_time'];
                $user['user_auth']=3;
                $user_info=new User();//创建数据对象
                if($user_info){//系统自动注册会员
                    $rec=$user_info->insertGetId($user);//建立会员信息
                    $new_data['donation_id']=$rec;
                    $new_data['donation_book']=$data['donation_book'];
                    $new_data['donation_price']=$data['donation_price'];
                    $new_data['get_coin']=$data['get_coin'];
                    $new_data['donation_time']=$data['donation_time'];
                    $new_data['donation_collect_id']=$data['donation_collect_id'];//当前
                    if($new_data){
                       Db::name('donation')->insert($new_data);// 建立捐赠记录
                        $student['student_id'] = $rec;
                        $stu_rec=Db::name('student')->insert($student);// 建立学生账户
                        if($stu_rec){
                            $coin=array();
                            $coin['receive_id'] = $new_data['donation_id'];
                            $coin['month_coin']=500;
                            $d_rec =Db::name('coin')->where(array('receive_id'=>$coin['receive_id']))->insert($coin);
                            if ($d_rec) {
                                echo json_encode(1);
                            }else{
                                echo json_encode(2);
                            }
                        } else {
                            echo json_encode(0);
                        }
                    }

                }
            }
        }
    }


    //查询自己的捐赠书籍学币领取信息
    public function self_donation(){
        //$_POST['id']=3;
        $id = $_POST['id'];
        //echo json_encode($_POST);
        if($_POST){
            $donations =Db::name('donation');//实例化donation对象
            $donation = $donations
                ->where('donation_id="' . $id . '" AND is_receive=0')
                ->order('id desc')
                ->select();
            echo json_encode($donation);
        }
    }

    /**
     *
     */
    public function check_order(){
        $status0['status']=0;
        $status1['status']=1;
        $status2['status']=2;
        $id = $_POST['id'];
        $code = $_POST['lesson_id'];
        $orders=Db::name('order');
        $order=$orders->where('lesson_id='.$code.' AND user_id='.$id)->find();
        $lesson =Db::name('lesson')->where('id='.$code.' AND is_approve=1')->find();
        if(!empty($order)){
          return json($status0);
        }else if(empty($lesson)){
           return json($status2);
        }else{
           return json($status1);
        }
    }
}